﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Psl.Chat;

namespace Stl.Chat.Components
{
    public partial class ChatView : UserControl
    {

        /************************************************************
         *                          Champs                          *
         ************************************************************/
        protected IChat modelInternal;

        protected int curMsgIdx = 0;


        /************************************************************
         *                        Constructeur                      *
         ************************************************************/
        /// <summary>
        /// Constructeur
        /// </summary>
        public ChatView()
        {// a revoir
            InitializeComponent();
            this.Enabled = false;
            this.modelInternal = ChatRegistry.ChatState.ModelInternal;
        }


        /************************************************************
         *                         Evenements                       *
         ************************************************************/

        private void chatEvents_ChatChanged(object sender, ChatChangedEventArgs e)
        {
            if (e.Clear)
            {
                DoClear();
                curMsgIdx = 0;
            }
            else
                DoAddItems(modelInternal.GetLasts(listChatView.Items.Count + curMsgIdx));
        }

        private void chatEvents_ChatOpened(object sender, EventArgs e)
        {
            if (modelInternal == null)
            {
                modelInternal = ChatRegistry.ChatState.ModelInternal;
                curMsgIdx = modelInternal.Count;
            }
            this.Enabled = true;
        }

        private void chatEvents_ChatClosing(object sender, EventArgs e)
        {
            this.Enabled = false;
        }

        /************************************************************
         *                          Méthodes                        *
         ************************************************************/
        protected void DoClear()
        {
            listChatView.Items.Clear();
        }

        protected void DoAddItem(int number, string userID, string msg)
        {
            ListViewItem item = listChatView.Items.Add(number.ToString());
            item.SubItems.Add(userID);
            item.SubItems.Add(msg);
            item.EnsureVisible();
        }

        protected void DoAddItems(ChatItem[] tabChatItem)
        {
            foreach (ChatItem item in tabChatItem)
            {
                DoAddItem(item.Chrono, item.UserID, item.Text);
            }
        }

        public void RefreshView()
        {
            DoClear();
            ChatItem[] messages = modelInternal.History;
            ChatItem msg;
            for (int i = curMsgIdx; i < messages.Length; i++)
            {
                msg = messages[i];
                DoAddItem(msg.Chrono, msg.UserID, msg.Text);
            }
        }

        public void LocalClear()
        {
            DoClear();
            curMsgIdx = modelInternal.Count;
        }

        public bool IsEmpty
        {
            get{ return listChatView.Items.Count == 0; }
        }
    }
}
